<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *公告管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$tabs=array(
	array('op'=>'list','title'=>'排课列表'),
	array('op'=>'edit','title'=>'添加排课'),
	array('op'=>'fx','title'=>'分校管理'),
	array('op'=>'pk','title'=>'排课管理'),
	array('op'=>'xs','title'=>'学生管理'),
	array('op'=>'qd','title'=>'签到管理'),
	array('op'=>'pj','title'=>'评价管理'),	
);
$return['status']=$this->lesson_status;
if($_RQ['op']=='fx'){

 
	exi('','','jy_kexie/store.index/','');

}
if($_RQ['op']=='pk'){

	 
	exi('','','jy_kexie/store.lesson_pai/','');

	
}
if($_RQ['op']=='xs'){

	
	exi('','','jy_kexie/store.user/','');


}
if($_RQ['op']=='qd'){

	
	exi('','','jy_kexie/store.sign//type=0','');


}
if($_RQ['op']=='pj'){

	exi('','','jy_kexie/store.comment//type=0','');

}
if($_RQ['op']=='list'){
	$params=array('storeid'=>$this->store_id);
	$params['uniacid']=$_SESSION['uniacid'];
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(!empty($_RQ['storeid'])){
		$params['storeid']=intval($_RQ['storeid']);
		$return['params']['storeid']=$_RQ['storeid'];
	}
	if(!empty($_RQ['groupid'])){
		$params['groupid']=intval($_RQ['groupid']);
		$return['params']['groupid']=$_RQ['groupid'];
	}
	if(!empty($_RQ['workerid'])){
		$params['workerid']=intval($_RQ['workerid']);
		$return['params']['workerid']=$_RQ['workerid'];
	}
	$nowyear = date('Y');
	if(empty($_RQ['year'])){
		$year = date('Y');
	}else{
		$year=$_RQ['year'];
	}
	$yeartime=strtotime($year.'-01');
	$lastyear=$year+1;
	$lasttime=strtotime($lastyear.'-01');
	$params['ontime between']=array($yeartime,$lasttime);
	$return['params']['year']=$year;
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('jy_kexie_lesson_pai',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		
		foreach($list as &$item){
			$item['lesson']=pdo_get('jy_kexie_lesson',array('id'=>$item['lessonid'],'uniacid'=>$_SESSION['uniacid']));
			$item['endtime']=$this->model('lesson')->get_endtime($item['ontime'],$item['lesson']['on_num'],$item['workerid']);
			$item['ontime']=$this->model('lesson')->get_uptime($item['ontime']);
			$item['worker']=pdo_get('jy_kexie_worker',array('id'=>$item['workerid'],'uniacid'=>$_SESSION['uniacid']));
			$item['is_num']=pdo_count('jy_kexie_lesson_pai',array('ontime between'=>array($yeartime,$lasttime),'storeid'=>$item['storeid'],'workerid'=>$item['workerid'],'lessonid'=>$item['lessonid']),'id');
			$item['last_num']=$item['lesson']['on_num']-$item['is_num'];
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'export'=>'export',
		'tabs'=>$tabs,
		'list'=>array('total'=>$total,'data'=>'list','edit'=>true,'tip'=>'报名课程','list'=>array(
			array('name'=>'workerid','title'=>'讲师','type'=>'select','options'=>'workers','option_title'=>'name'),
			array('name'=>'worker.mobile','title'=>'讲师电话','type'=>'string'),
			array('name'=>'lesson.title','title'=>'课程名称','type'=>'string'),
			//array('name'=>'area_id','title'=>'所属区域','type'=>'select','options'=>'areas'),
			array('name'=>'lesson.groupid','title'=>'课程类型','type'=>'select','options'=>'groups'),
			//array('name'=>'onmonth','title'=>'上课月份','type'=>'string'),
			array('name'=>'onday','title'=>'上课时间','type'=>'string'),
			array('name'=>'last_num','title'=>'剩余待选课时数','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
		),'action'=>array(
			array('title'=>'讲师介绍','type'=>'modal','op'=>'look'),
		)),
		'search'=>array(
			//array('name'=>'area_id','width'=>10,'title'=>'所属区域','type'=>'select','options'=>'areas'),
			array('name'=>'year','width'=>10,'title'=>'所属年份','type'=>'select','options'=>'years'),
			array('name'=>'groupid','width'=>10,'title'=>'课程类型','type'=>'select','options'=>'groups'),
			array('name'=>'workerid','width'=>10,'title'=>'讲师','type'=>'select_more','options'=>'workers','option_title'=>'name'),
			array('name'=>'title','width'=>10,'title'=>'课程名称','type'=>'string'),
		)
	);
	$return['htmls']=$htmls;
	$nowyear = date('Y');
	for($i=1;$i<=10;$i++){
		$now=$nowyear-(10-$i);
		$return['years'][]=array('id'=>$now,'title'=>$now);
	}
	for($i=1;$i<=2;$i++){
		$now=$nowyear+$i;
		$return['years'][]=array('id'=>$now,'title'=>$now);
	}
	$return['stores']=pdo_getall('jy_kexie_store',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['areas']=pdo_getall('jy_kexie_area',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['groups']=pdo_getall('jy_kexie_lesson_group',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['workers']=pdo_getall('jy_kexie_worker',array('uniacid'=>$_SESSION['uniacid']),array('id','name'),'id');
	set_html('title','排课后台');
	exi($return);
}
$steps=array('选择讲师','排课日期');
if($_RQ['op']=='edit'){
	$return['step']=1;
	if($_RQ['postdata']){
		$postdata=get_postdata();
		if(!$postdata['groupid'] || !$postdata['workerid']){
			exi('请选择课程类型,讲师！','error');
		}
		exi('','','/store.lesson_pai/step2/id='.$postdata['id'].'&groupid='.$postdata['groupid'].'&workerid='.$postdata['workerid']);
	}
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_kexie_lesson_pai',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		$item['ontime']=date('Y-m-d H:i:s',$item['ontime']);
		$item['lessontitle']=pdo_getcolumn('jy_kexie_lesson',array('id'=>$item['lessonid'],'uniacid'=>$_SESSION['uniacid']),'title');
		$item['lessondesc']=pdo_getcolumn('jy_kexie_worker',array('id'=>$item['workerid'],'uniacid'=>$_SESSION['uniacid']),'desc');
	}else{
		$item=array();
	}
	$item['week_days']=$store['week_days']?:0;
	$item['isstore']=1;
	$item['week_daystitle']=$this->week_days[$store['week_days']];
	$this->model('lesson')->checkpaike($store);
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回课程列表'),
		),
		'submit_title'=>'保存并下一步',
		'edit'=>array(
			array('title'=>'[温馨提示]：只显示上课时间，所属区域都满足条件的讲师！','type'=>'tip'),
			array('name'=>'groupid','title'=>'课程类型','type'=>'select','options'=>'groups'),
			array('name'=>'workerid','title'=>'讲师选择','type'=>'select','option_title'=>'name','options'=>'workers[pagedata.item.groupid]','help'=>'只显示本学校，所属区域的讲师！'),
		)
	);
	$return['storetype']=$this->storetype;
	$return['areas']=pdo_getall('jy_kexie_area',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['stores']=pdo_getall('jy_kexie_store',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['groups']=pdo_getall('jy_kexie_lesson_group',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$workers=array();
	foreach($return['groups'] as &$gv){
		$workers[$gv['id']]=pdo_getall('jy_kexie_worker',array('lesson_groupid'=>$gv['id'],'status'=>1,'uniacid'=>$_SESSION['uniacid']),array('id','name'));
	}
	$return['workers']=$workers;
	$return['area_id']=$store['area_id'];
	$return['htmls'][]=array('type'=>'steps','steps'=>$steps,'step'=>'1');
	$return['htmls'][]=$htmls;
	exi($return);
}
if($_RQ['op']=='step2'){
	$return['step']=2;
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_kexie_lesson_pai',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		$item['ontime']=date('Y-m-d H:i:s',$item['ontime']);
	}else{
		$item=array();
	}
	$item['groupid']=$_RQ['groupid'];
	$item['workerid']=$_RQ['workerid'];
	$item['grouptitle']=pdo_getcolumn('jy_kexie_lesson_group',array('id'=>$item['groupid'],'uniacid'=>$_SESSION['uniacid']),'title');
	$item['workername']=pdo_getcolumn('jy_kexie_worker',array('id'=>$item['workerid'],'uniacid'=>$_SESSION['uniacid']),'name');
	$item['lessonid']=pdo_getcolumn('jy_kexie_worker',array('id'=>$item['workerid'],'uniacid'=>$_SESSION['uniacid']),'lesson_id');
	$item['lessontitle']=pdo_getcolumn('jy_kexie_lesson',array('id'=>$item['lessonid'],'uniacid'=>$_SESSION['uniacid']),'title');
	$item['lessondesc']=pdo_getcolumn('jy_kexie_worker',array('id'=>$item['workerid'],'uniacid'=>$_SESSION['uniacid']),'desc');
	$item['week_days']=$store['week_days']?:0;
	$item['isstore']=1;
	$item['week_daystitle']=$this->week_days[$store['week_days']];
	$this->model('lesson')->checkpaike($store);
	$item['workerid2']=$item['workerid'];
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'edits'=>array(
			array(
				'title'=>'基本信息',
				'list'=>array(
					array('title'=>'[温馨提示]：只显示上课时间，所属区域都满足条件的讲师！','type'=>'tip'),
					array('name'=>'week_days','title'=>'上课时间','type'=>'string','read'=>true,'show'=>array('week_days >'=>'0')),
					array('name'=>'grouptitle','title'=>'课程类型','type'=>'string','read'=>true),
					array('name'=>'workername','title'=>'讲师','type'=>'string','read'=>true),
					array('name'=>'lessontitle','title'=>'课程名称','type'=>'string','read'=>true),
					array('name'=>'lessondesc','title'=>'课程简介','type'=>'string','read'=>true),
					array('name'=>'onmonth','title'=>'上课月份','type'=>'select2','options'=>'onmonths',
						'title2'=>'上课时间','name2'=>'onday','options2'=>'list',
					),
					array('name'=>'paixu','title'=>'排序','type'=>'string'),
					//array('name'=>'status','title'=>'状态','type'=>'switch'),
				)
			),
		)
	);
	$return['storetype']=$this->storetype;
	//$return['workers']=$workers;
	$return['area_id']=$store['area_id'];
	//$workers=pdo_getall('jy_kexie_worker',array('on_areas like'=>'%;'.trim($store['area_id']).';%','status'=>1,'uniacid'=>$_SESSION['uniacid']),array('id','name'),'id');
	$onmonths=$this->model('lesson')->get_months($item['workerid'],'','',$item);
	$return['onmonths']=$onmonths;
	$return['htmls'][]=array('type'=>'steps','steps'=>$steps,'step'=>'2');
	$return['htmls'][]=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if(!$postdata['lessonid']){
		//exi('请选择课程！','error');
	}
	if(!$postdata['storeid']){
		//exi('请选择校区！','error');
	}
	if(!$postdata['onday']){
		exi('请选择上课时间！','error');
	}
	$postdata['workerid']=$postdata['workerid']?:$postdata['workerid2'];
	if(!$postdata['workerid']){
		exi('请选择讲师！','error');
	}
 
	$data=array_fields(array('onmonth','onday','ontype','ontime','groupid','lessonid','workerid','storetype','person_limit','paixu','status'),$postdata);
	$id=intval($postdata['id']);

	 
	
	$data['ontime']=strtotime($data['onday']);
	$data['weekid']=date("w",$data['ontime']);
	$data['lessonid']=pdo_getcolumn('jy_kexie_worker',array('id'=>$postdata['workerid'],'uniacid'=>$_SESSION['uniacid']),'lesson_id');
	$data['groupid']=pdo_getcolumn('jy_kexie_lesson',array('id'=>$data['lessonid'],'uniacid'=>$_SESSION['uniacid']),'groupid');
	$lesson=pdo_get('jy_kexie_lesson',array('id'=>$data['lessonid'],'uniacid'=>$_SESSION['uniacid']));
	$year = date('Y',$data['ontime']);
	$yeartime=strtotime($year.'-01');
	$lastyear=$year+1;
	$lasttime=strtotime($lastyear.'-01');
	$isnum=pdo_count('jy_kexie_lesson_pai',array('ontime between'=>array($yeartime,$lasttime),'storeid'=>$this->store_id,'workerid'=>$data['workerid'],'lessonid'=>$data['lessonid']),'id');
	if($lesson['on_num']<=$isnum){
		exi('该讲师的该课程总课时['.$lesson['on_num'].'],已经选完！','error');
	}
	$isonday=pdo_get('jy_kexie_lesson_pai',array('onday'=>$data['onday'],'storeid'=>$this->store_id,'uniacid'=>$_SESSION['uniacid']));
	if($isonday['id']){
		exi('该校区当天已经排其他课了！','error');
	}
	if($id){
		$data['storeid']=$this->store_id;
		$data['area_id']=$store['area_id'];
		pdo_update('jy_kexie_lesson_pai',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['storeid']=$this->store_id;
		$data['area_id']=$store['area_id'];
		if($return['pluginset']['no_shenhe']){
			$data['status']=1;	
		}
		pdo_insert('jy_kexie_lesson_pai',$data);
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='export'){
	$params=array('storeid'=>$this->store_id,'uniacid'=>$_SESSION['uniacid']);
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(!empty($_RQ['area_id'])){
		$params['area_id']=intval($_RQ['area_id']);
		$return['params']['area_id']=$_RQ['area_id'];
	}
	if(!empty($_RQ['storeid'])){
		$params['storeid']=intval($_RQ['storeid']);
		$return['params']['storeid']=$_RQ['storeid'];
	}
	if(!empty($_RQ['groupid'])){
		$params['groupid']=intval($_RQ['groupid']);
		$return['params']['groupid']=$_RQ['groupid'];
	}
	if(!empty($_RQ['workerid'])){
		$params['workerid']=intval($_RQ['workerid']);
		$return['params']['workerid']=$_RQ['workerid'];
	}
	if(empty($_RQ['year'])){
		$year = date('Y');
	}else{
		$year=$_RQ['year'];
	}
	$yeartime=strtotime($year.'-01');
	$lastyear=$year+1;
	$lasttime=strtotime($lastyear.'-01');
	$params['ontime between']=array($yeartime,$lasttime);
	$lesson=pdo_getall('jy_kexie_lesson_pai',$params);
	foreach($lesson as &$item){
			$item['lessontitle']=pdo_getcolumn('jy_kexie_lesson',array('id'=>$item['lessonid'],'uniacid'=>$_SESSION['uniacid']),'title');
			$item['workerdesc']=pdo_getcolumn('jy_kexie_worker',array('id'=>$item['workerid'],'uniacid'=>$_SESSION['uniacid']),'show');
			$item['workertitle']=pdo_getcolumn('jy_kexie_worker',array('id'=>$item['workerid'],'uniacid'=>$_SESSION['uniacid']),'name');
			$item['storeaddress']=pdo_getcolumn('jy_kexie_store',array('id'=>$item['storeid'],'uniacid'=>$_SESSION['uniacid']),'address');
			$item['is_num']=pdo_count('jy_kexie_lesson_pai',array('storeid'=>$item['storeid'],'workerid'=>$item['workerid'],'lessonid'=>$item['lessonid']),'id');
			$item['last_num']=$item['lesson']['on_num']-$item['is_num'];
	}
	$header=array(
		'onday'=>'日期',
		'lessontitle'=>'课程',
		'workertitle'=>'授课人',
		'workerdesc'=>'授课内容',
		'storeaddress'=>'授课地点',
	);
	cfc('tools')->export2excel($header,$lesson,'课程表');
}
if($_RQ['op']=='look'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_kexie_lesson_pai',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		$item=pdo_get('jy_kexie_worker',array('id'=>$item['workerid'],'uniacid'=>$_SESSION['uniacid']));
		unset($item['password']);
	}else{
		$item=array();
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'look',
		'look'=>array(
					array('name'=>'headimg','title'=>'头像','type'=>'image'),
					array('name'=>'name','title'=>'姓名','type'=>'string'),
					array('name'=>'mobile','title'=>'手机号','type'=>'string'),
					array('name'=>'tel','title'=>'固定电话','type'=>'string'),
					array('name'=>'sex','title'=>'性别','type'=>'radio','options'=>'sex'),
					array('name'=>'years','title'=>'出生年月','type'=>'time_date'),
					array('type'=>'checkbox','title'=>'授课时间','name'=>'on_days','help'=>'','options'=>'on_days'),
					array('name'=>'on_num','title'=>'上课次数','type'=>'string'),
					array('name'=>'address','title'=>'现居住地','type'=>'string'),
					array('name'=>'show','title'=>'讲课内容','type'=>'text'),
					array('name'=>'desc','title'=>'教师简介','type'=>'richtext'),
					array('name'=>'lessondesc','title'=>'课程简介','type'=>'richtext'),
					array('name'=>'remark','title'=>'备注','type'=>'text'),
		)
	);
	$return['on_days']=array('0'=>'周日','1'=>'周一','2'=>'周二','3'=>'周三','4'=>'周四','5'=>'周五','6'=>'周六');
	$return['sex']=array('1'=>'男','2'=>'女');
	$return['status']=array('0'=>'关闭','1'=>'开启');
	$return['areas']=pdo_getall('jy_kexie_area',array('uniacid'=>$_SESSION['uniacid']));
	$return['lesson']=pdo_getall('jy_kexie_lesson_group',array('uniacid'=>$_SESSION['uniacid']));
	$return['htmls']=$htmls;	
	exi($return);
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_kexie_lesson_pai',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要编辑的课程！','error');
	}
	$post=get_postdata();
	if(isset($post['status'])){
		pdo_update('jy_kexie_lesson_pai',array('status'=>intval($post['status'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}